package com.itheima.dubbo.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author 阿鹏
 * @date 2021/1/24 23:05
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        Object created = this.getFieldValByName("created", metaObject);
        if (created == null) {
            setFieldValByName("created", new Date(), metaObject);
        }
        Object updated = this.getFieldValByName("updated", metaObject);
        if (updated == null) {
            setFieldValByName("updated", new Date(), metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        // 只要更新，就更新updated字段
        this.setFieldValByName("updated", new Date(), metaObject);
    }
}
